"""
split from main.py

flask server mainClass
"""

from sys import path
path.append('../config_param')

import time
import log_proc as log
from basic_operations import BasicOperations
from main_line import MainLine
from retrieve_fireworks import RetrieveFireworks
from constants import *

import InputParams as inputParams
import retrieve_fiework_param as rf_params

# 错误判断修改
# 点击默认区域
# 检测“本丸”图标，检测到后点击
# while检测本丸，重复10 次
class ServerClass:
    def __init__(self):
        self.basic = BasicOperations()
        log.info("================================shell start=====================================")
        time.sleep(10)
        maxLoc, bottomRight = basic.checkLocation(NAME_HOST)
        while len(maxLoc) == 0:
            maxLoc, bottomRight = basic.checkLocation(NAME_HOST)
        print("init ok")

    def serverStart(self):
        #初始化脚本，等待本丸主界面

        # 主线练级
        mainLineUpgrade(basic)

        # 夜花夺还活动
        retrieveFireworks(basic)

        #  联队战
        # 1. 检测到主界面，进入活动启动界面，选择队伍（可输入），按下启动按钮
        # 2. 等待40s
        # 3. 检测队伍出发界面
        # 4. 检测重伤和刀装数量
        # 5. 根据数量，选择出发，恢复，换队，回城， 目前要做的是出发，换队和回城，如果没有队伍可换就回城
        # 6. 回城后，安全区域点击，等待x秒，再检测主界面，选择活动。检测不到，再选择安全区域点击，等待检测
        # 7. 检测到主界面，重复1


    def resetHost(self):
        basic = self.basic
        max_loc1 = []
        attempt = 0

        while len(max_loc1) == 0 and attempt < ATTLIMIT:
            # 1.检测有没有 本丸
                # -》 有，点击一次默认位置，就返回True，重新开始循环
            # 2.没有检测到， 检测有没有“本丸”按钮，有则点击， 回到 #1
            # 3. 都没有，点击一下默认位置，回到#1
            max_loc1, bottom_right1 = basic.checkLocation(NAME_HOST)

            if len(max_loc1) != 0:
                basic.inputLocationAndClick(DEFAULT_TOPLOC,DEFAULT_BOTLOC)
                time.sleep(1)
                return True
            else:
                max_loc3, bottom_right3 = basic.checkLocation(NAME_RETURNHOST)
                if len(max_loc3) != 0:
                    basic.inputLocationAndClick(max_loc3, bottom_right3)
                    time.sleep(1)
                else:
                    max_loc2, bottom_right2 = basic.checkLocation(NAME_RESET)
                    if len(max_loc2) != 0:
                        basic.inputLocationAndClick(max_loc2, bottom_right2)
                        time.sleep(1)
                    else:
                        basic.inputLocationAndClick(DEFAULT_TOPLOC, DEFAULT_BOTLOC)
                        time.sleep(1)
            attempt = attempt + 1
        return False

# 日常练级
    def mainLineUpgrade(self):
        basic = self.basic
        epoch = 0
        upgradeTask = MainLine(inputParams.mapNo, inputParams.difficulty, inputParams.teamNo,
                            inputParams.steps, inputParams.borderNum)
        while epoch < inputParams.upgradeNum:
            log.info("………………………………开始第" + str(epoch + 1) + "次出阵………………………………")
            if not upgradeTask.upgradeOnce():
                log.info("出阵失败，重置到本丸")
                if not resetHost(basic):
                    log.info("重置失败，退出程序")
                    exit(0)
            basic.removeTempShotList()
            epoch = epoch + 1

# 夜花作战
    def retrieveFireworks(self):
        basic = self.basic
        epoch = 0
        rfTask = RetrieveFireworks(rf_params.teamNo)
        while epoch < rf_params.upgradeNum:
            log.info("………………………………开始第" + str(epoch + 1) + "次出阵………………………………")
            if not rfTask.retrieveOnce():
                log.info("出阵失败，重置到本丸")
                if not resetHost(basic):
                    log.info("重置失败，退出程序")
                    exit(0)
            basic.removeTempShotList()
            epoch = epoch + 1

    def testServer(self):
        print("hello world")